//
//  ChangePasswordViewController.m
//  cyydt
//
//  Created by 959 on 2018/12/25.
//  Copyright © 2018 959. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()
{
    NSInteger _countDown;
    NSTimer *_timer;
}
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *captchaView;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIView *confirmPasswordView;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmPWTF;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation ChangePasswordViewController
- (void)dealloc{
    [_timer invalidate];
    _timer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _countDown = 60;
    self.title= @"修改密码";
    self.phoneView.layer.borderWidth = 1;
    self.phoneView.layer.borderColor = RGBOF(0xe3e3e3).CGColor;
    self.phoneView.layer.masksToBounds = YES;
    self.phoneView.layer.cornerRadius = 25;
    
    self.captchaView.layer.borderWidth = 1;
    self.captchaView.layer.borderColor = RGBOF(0xe3e3e3).CGColor;
    self.captchaView.layer.masksToBounds = YES;
    self.captchaView.layer.cornerRadius = 25;
    
    self.passwordView.layer.borderWidth = 1;
    self.passwordView.layer.borderColor = RGBOF(0xe3e3e3).CGColor;
    self.passwordView.layer.masksToBounds = YES;
    self.passwordView.layer.cornerRadius = 25;
    
    self.confirmPasswordView.layer.borderWidth = 1;
    self.confirmPasswordView.layer.borderColor = RGBOF(0xe3e3e3).CGColor;
    self.confirmPasswordView.layer.masksToBounds = YES;
    self.confirmPasswordView.layer.cornerRadius = 25;
    
    self.confirmBtn.layer.masksToBounds = YES;
    self.confirmBtn.layer.cornerRadius = 25;
}
- (IBAction)codeAction:(UIButton *)sender {
    //先判断手机号
    if (self.phoneTF.text.length <= 0) {
        [self.view makeToast:@"请输入您的手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    [self getVerify];
}

- (void)getVerify{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"code"];
    NSDictionary *param = @{@"phone":self.phoneTF.text};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"验证码 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
            self.codeBtn.enabled = YES;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self->_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }else{
                self.codeBtn.enabled = YES;
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

#pragma mark - PrivateMethod
- (void)onTimer{
    if (_countDown > 0) {
        [self.codeBtn setTitle:[NSString stringWithFormat:@"%ld秒重新获取", (long)_countDown] forState:UIControlStateNormal];
        _countDown--;
    }else{
        _countDown = 60;
        [_timer invalidate];
        _timer = nil;
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeBtn.enabled = YES;
    }
}

- (IBAction)doneAction:(UIButton *)sender {
    if ([NSString stringIsNull:self.phoneTF.text]) {
        [self.view makeToast:@"请输入手机号码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.codeTF.text]) {
        [self.view makeToast:@"请输入验证码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.passwordTF.text]) {
        [self.view makeToast:@"请输入您的密码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.confirmPWTF.text]) {
        [self.view makeToast:@"请再次输入您的密码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"user/findPassword"];
    NSDictionary *param = @{@"phone":self.phoneTF.text, @"captcha":self.codeTF.text, @"password":self.passwordTF.text, @"repassword":self.confirmPWTF.text};
    [EasyShowLodingView showLoding];
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"找回密码 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [[AppDelegate sharedAppDelegate] popToRootViewController];
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
